package com.credit.web.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.persistence.SearchDataTransform;

import com.aliyuncs.exceptions.ClientException;
import com.credit.context.CreditServletContext;
import com.credit.entity.CreditMessageEntity;
import com.credit.entity.CreditUserInfo;
import com.credit.exception.ErrorException;
import com.credit.service.CreditMessageService;
import com.credit.util.BeanToMapUtil;
import com.credit.util.ParamsConstant;
import com.credit.web.basic.bean.ResBean;

@Controller
@RequestMapping("/message")
public class ApiMessageController {
	@Autowired
	private CreditMessageService creditMessageService;
	
	@ResponseBody
	@RequestMapping(value = "/notify")
	public void notify(HttpServletRequest request, HttpServletResponse response)
			throws ErrorException, IllegalArgumentException, IllegalAccessException, ClientException {

		CreditUserInfo userInfo = CreditServletContext.getUserInfo();
		
		String page = request.getParameter("page");
		
		Map<String, Object> searchParams = new TreeMap<String, Object>();
		searchParams.put("EQ_userId", userInfo.getId());

		Map<String, SearchDataTransform> dataTyps = new HashMap<String, SearchDataTransform>();
		
		Page<CreditMessageEntity> messagePage = creditMessageService.getMessageListPage(searchParams,Integer.valueOf(page),ParamsConstant.PAGESIZE,"createTime",dataTyps);
		
		List<CreditMessageEntity> list = messagePage.getContent();
		
		List<Map<String,Object>> messageListReturn = new ArrayList<Map<String,Object>>();
		for(CreditMessageEntity CreditMessageEntity:list){
			Map<String,Object> returnMap = BeanToMapUtil.getMap(CreditMessageEntity);
			messageListReturn.add(returnMap);
		}
		
		ResBean resBean = CreditServletContext.getResBean();
		resBean.getData().put("messageList", messageListReturn);
		CreditServletContext.setResBean(resBean);
		
	}

}
